<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24"> </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button style="margin-right: 20px" @click="addOrder()" type="primary"> 新增绑定订单</a-button>
      <a-button type="primary" icon="download" @click="handleExport1()">导出全部订单</a-button>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>

      <!-- 高级查询区域 -->
      <j-super-query
        :fieldList="superFieldList"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
      ></j-super-query>

      <!-- <a-select
        class="search-input"
        showSearch
        :showArrow="false"
        placeholder="搜索菜单"
        optionFilterProp="children"
        :filterOption="filterOption"
        :open="isMobile() ? true : null"
        :getPopupContainer="(node) => node.parentNode"
        :style="isMobile() ? { width: '100%', marginBottom: '50px' } : {}"
        @change="searchMethods"
        @blur="hiddenClick"
      >
        <a-select-option v-for="(site, index) in searchMenuOptions" :key="index" :value="site.id">{{
          site.meta.title
        }}</a-select-option>
      </a-select> -->

      <a-row>
        <a-col :span="6">
          <div style="margin-right: 12px">
            <a-form-model-item prop="warehourse">
              <a-select
                showSearch
                v-model="queryParam.warehourse"
                style="width: 100%; margin-top: 4px"
                placeholder="请选择所属仓库"
                allowClear
              >
                <a-select-option v-for="(item, index) in warehouseOptions" :key="index" :value="item.label">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </div>
        </a-col>
        <a-button style="margin-top: 4px" type="primary" icon="download" @click="exportWarehouseFile()">
          按仓库导出
        </a-button>
        <a-button style="margin-top: 4px" type="primary" icon="download" @click="exportAllFlies()"> 全量导出 </a-button>
      </a-row>

      <!-- <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->

      <!-- 仓库分配 -->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="orderType" slot-scope="text">
          <!--  text.orderType * 1 === 3 ? '离线模式' : 'wife' -->
          {{ getOrderType(text.orderType) }}
        </template>

        <template slot="state" slot-scope="text">
          <!--  text.orderType * 1 === 3 ? '离线模式' : 'wife' -->
          {{ getOrderState(text.state) }}
        </template>

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <!--<a @click="handleEdit(record)">编辑</a>  -->
          <a @click="handleExport(record)">导出</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <wms-bar-bind-order-modal ref="modalForm" @ok="modalFormOk1"></wms-bar-bind-order-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import WmsBarBindOrderModal from './modules/WmsBarBindOrderModal'
import { mapState } from 'vuex'
import { stateObjToArray } from '@/utils/common.js'
import { deleteAction, getAction, downFile, getFileAccessHttpUrl } from '@/api/manage'

export default {
  name: 'WmsBarBindOrderList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    WmsBarBindOrderModal,
  },
  data() {
    return {
      description: 'wms_bar_bind_order管理页面',
      relWarehouseIds: '',
      validatorRules: {
        relWarehouseIds: [{ required: false, message: '请选择!' }],
      },

      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 9 },
      },
      disableSubmit: false,
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '本地订单号',
          align: 'center',
          dataIndex: 'localOrderNo',
        },
        {
          title: '订单号',
          align: 'center',
          dataIndex: 'orderNo',
        },
        {
          title: '订单类型',
          align: 'center',
          //dataIndex: 'orderType'
          scopedSlots: { customRender: 'orderType' },
        },
        {
          title: '状态',
          align: 'center',
          //dataIndex: 'state'
          scopedSlots: { customRender: 'state' },
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'sums',
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy',
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime',
        },

        {
          title: '所属仓库',
          align: 'center',
          dataIndex: 'wareName',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/wms/wmsBarBindOrder/list',
        delete: '/wms/wmsBarBindOrder/delete',
        deleteBatch: '/wms/wmsBarBindOrder/deleteBatch',
        exportXlsUrl: '/wms/wmsBarBindOrder/exportXls',
        importExcelUrl: 'wms/wmsBarBindOrder/importExcel',
        exportAllFilesUrl: 'wms/wmsBarBindOrder/allExportSpecXls',
        exportFilesUrl: 'wms/wmsBarBindOrder/exportWareInfo',
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    ...mapState({
      warehouseOptions: (s) => {
        return stateObjToArray(s.dictionary.enableWarehouse)
      },
    }),
  },
  methods: {
    getOrderType(orderType) {
      if (orderType === '3') {
        return '离线绑定'
      } else if (orderType === '0') {
        return 'wife-一物一码'
      } else if (orderType === '1') {
        return 'wife-一码多物'
      } else {
        return '线上绑定'
      }
    },
    exportWarehouseFile() {
      if (!this.queryParam.warehourse && this.queryParam.warehourse != '') {
        return this.$message.warning('请选择仓库')
      }
      try {
        let that = this
        let param = this.getQueryParams()
        if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
          param['selections'] = this.selectedRowKeys.join(',')
        }
        console.log('导出参数', param)
        downFile(this.url.exportFilesUrl, {
          wareName: that.queryParam.warehourse,
        }).then((data) => {
          if (!data) {
            this.$message.warning('文件下载失败')
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(
              new Blob([data], { type: 'application/vnd.ms-excel' }),
              that.relWarehouseIds + '.xls'
            )
          } else {
            let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', that.relWarehouseIds + '.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link) //下载完成移除元素
            window.URL.revokeObjectURL(url) //释放掉blob对象
          }
        })
      } catch (err) {
        console.log(err)
      }
    },
    modalFormOk1() {
      // 新增/修改 成功时，重载列表
      this.loadData1()
      //清空列表选中
      this.onClearSelected()
    },

    getOrderState(state) {
      if (state === '1') {
        return '已完成'
      } else if (state === '0') {
        return '已创建'
      } else {
        return '草稿'
      }
    },
    handleExport(e) {
      // console.log('handleExport', e)
      this.handleExportXls('一品一码绑定_' + e.orderNo, { id: e.id, field: '' })
    },
    handleExport1(e) {
      this.handleExportXls('一品一码绑定')
    },
    exportAllFlies() {
      try {
        let that = this
        let param = this.getQueryParams()
        if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
          param['selections'] = this.selectedRowKeys.join(',')
        }
        console.log('导出参数', param)
        downFile(this.url.exportAllFilesUrl, {}).then((data) => {
          if (!data) {
            this.$message.warning('文件下载失败')
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(
              new Blob([data], { type: 'application/vnd.ms-excel' }),
              that.relWarehouseIds + '.xls'
            )
          } else {
            let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', that.relWarehouseIds + '.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link) //下载完成移除元素
            window.URL.revokeObjectURL(url) //释放掉blob对象
          }
        })
      } catch (err) {
        console.log(err)
      }
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'orderNo', text: '订单号' })
      fieldList.push({ type: 'string', value: 'orderType', text: '订单类型' })
      fieldList.push({ type: 'string', value: 'state', text: '状态' })
      fieldList.push({ type: 'int', value: 'sums', text: '数量' })
      fieldList.push({ type: 'string', value: 'wareName', text: '所属仓库' })
      this.superFieldList = fieldList
    },
    addOrder() {
      this.$router.push({
        path: '/in-warehouse-order-add',
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
